package cn.iocoder.yudao.module.lfd.controller.admin.appointmentitem;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.lfd.controller.admin.appointmentitem.vo.*;
import cn.iocoder.yudao.module.lfd.dal.dataobject.appointmentitem.AppointmentItemDO;
import cn.iocoder.yudao.module.lfd.convert.appointmentitem.AppointmentItemConvert;
import cn.iocoder.yudao.module.lfd.service.appointmentitem.AppointmentItemService;

@Tag(name = "管理后台 - 预约项目")
@RestController
@RequestMapping("/lfd/appointment-item")
@Validated
public class AppointmentItemController {

    @Resource
    private AppointmentItemService appointmentItemService;

    @PostMapping("/create")
    @Operation(summary = "创建预约项目")
    @PreAuthorize("@ss.hasPermission('lfd:appointment-item:create')")
    public CommonResult<Long> createAppointmentItem(@Valid @RequestBody AppointmentItemCreateReqVO createReqVO) {
        return success(appointmentItemService.createAppointmentItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新预约项目")
    @PreAuthorize("@ss.hasPermission('lfd:appointment-item:update')")
    public CommonResult<Boolean> updateAppointmentItem(@Valid @RequestBody AppointmentItemUpdateReqVO updateReqVO) {
        appointmentItemService.updateAppointmentItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除预约项目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('lfd:appointment-item:delete')")
    public CommonResult<Boolean> deleteAppointmentItem(@RequestParam("id") Long id) {
        appointmentItemService.deleteAppointmentItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得预约项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('lfd:appointment-item:query')")
    public CommonResult<AppointmentItemRespVO> getAppointmentItem(@RequestParam("id") Long id) {
        AppointmentItemDO appointmentItem = appointmentItemService.getAppointmentItem(id);
        return success(AppointmentItemConvert.INSTANCE.convert(appointmentItem));
    }

    @GetMapping("/list")
    @Operation(summary = "获得预约项目列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('lfd:appointment-item:query')")
    public CommonResult<List<AppointmentItemRespVO>> getAppointmentItemList(@RequestParam("ids") Collection<Long> ids) {
        List<AppointmentItemDO> list = appointmentItemService.getAppointmentItemList(ids);
        return success(AppointmentItemConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得预约项目分页")
    @PreAuthorize("@ss.hasPermission('lfd:appointment-item:query')")
    public CommonResult<PageResult<AppointmentItemRespVO>> getAppointmentItemPage(@Valid AppointmentItemPageReqVO pageVO) {
        PageResult<AppointmentItemDO> pageResult = appointmentItemService.getAppointmentItemPage(pageVO);
        return success(AppointmentItemConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出预约项目 Excel")
    @PreAuthorize("@ss.hasPermission('lfd:appointment-item:export')")
    @OperateLog(type = EXPORT)
    public void exportAppointmentItemExcel(@Valid AppointmentItemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AppointmentItemDO> list = appointmentItemService.getAppointmentItemList(exportReqVO);
        // 导出 Excel
        List<AppointmentItemExcelVO> datas = AppointmentItemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "预约项目.xls", "数据", AppointmentItemExcelVO.class, datas);
    }

}
